package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.ResponseUtil;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    //list?page=1&limit=20&username=aa&sort=add_time&order=desc
    @RequiresPermissions(value = "admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")
    @GetMapping("/list")
    public Object list(String username, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String sort, String order) {

        IPage<DtsAdmin> iPage = adminService.findListByQuery(username,page,limit,sort,order);


        Map data = new HashMap();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());


        return ResponseUtil.ok(data);
    }
    @RequiresPermissions(value = "admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin dtsAdmin){
        dtsAdmin = adminService.create(dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);
    }


    /**
     * addTime
     * 1677136908000
     * avatar
     * "http://qf2209.oss-cn-beijing.aliyuncs.com/1677210671968.png?Expires=1678290579&OSSAccessKeyId=LTAI5tPL88fRPnZBSuKz9JwL&Signature=EHZxK9GLfZakot1UcrM3IKu71Qk%3D"
     * deleted
     * false
     * desc
     * null
     * id
     * 11
     * lastLoginIp
     * ""
     * lastLoginTime
     * null
     * mail
     * null
     * password
     * "123456"
     * roleIds
     * [4]
     * tel
     * null
     * updateTime
     * 1677136908000
     * username
     * "2209"*/
    @RequiresPermissions(value = "admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin dtsAdmin){
        dtsAdmin.setUpdateTime(new Date());
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
        adminService.updateAdmin(dtsAdmin);
        return ResponseUtil.ok();
    }



    /**
     * addTime
     * 1677136908000
     * avatar
     * "'"
     * deleted
     * false
     * desc
     * null
     * id
     * 11
     * lastLoginIp
     * ""
     * lastLoginTime
     * null
     * mail
     * null
     * password
     * "$2a$10$P/kmafWMYW5jqK1fBfjnSO9qd3UFVsAU/Ju8RYcOUw2mHZ9stXXlq"
     * roleIds
     * [4]
     * tel
     * null
     * updateTime
     * 1677136908000
     * username
     * "2209"
     *
     * */
    @RequiresPermissions(value = "admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin){
        dtsAdmin.setDeleted(true);
        adminService.deleteAdmin(dtsAdmin.getId(),dtsAdmin.getDeleted());
        return ResponseUtil.ok();
    }
}
